<template>
  <div class="container">
    <Navbar title="VIP会员"></Navbar>
    <div class="content" v-if="finish">
      <div class="user-info">
        <div class="avatar">
          <img :src="userInfo.avatar" alt="">
        </div>
        <div class="role-info">
          <p>您的{{userInfo.role.name}}{{userInfo.role_expired_at}}到期</p>
          <span>购买后将会有效期顺延</span>
        </div>
      </div>
      <div class="role">
        <div class="role-item" v-for="item in roles" :key="item.id" @click="chooseCurrent(item)" :class="{current: item.id==current.goods_id}">
          <div class="name">{{item.name}}</div>
          <div class="price"><span>￥</span>{{item.charge}}</div>
          <div class="desc">{{item.desc_rows[0]}}</div>
        </div>
      </div>
    </div>
    <div class="buy" @click="buy">购买会员</div>
  </div>
</template>

<script>
import Navbar from '@/components/common/Navbar';
import { getUser, getRoles } from '@/network/member';
import { Notify } from 'vant';

export default {
  components: {
    Navbar,
  },
  data() {
    return {
      userInfo: {
        role: '',
      },
      roles: [],
      finish: false,
      current: '',
    };
  },
  methods: {
    chooseCurrent(item) {
      this.current = {
        goods_id: item.id,
        goods_name: item.name,
        goods_label: 'VIP会员',
        goods_charge: item.charge,
        goods_type: 'role',
      };
    },
    buy() {
      if (!this.current) return Notify('请选择需要购买的会员');
      this.$router.push({
        path: '/order',
        query: this.current,
      });
    },
  },
  created() {
    getUser().then((res) => {
      this.userInfo = res.data;
      this.role = res.data.role;
    });
    getRoles().then((res) => {
      this.roles = res.data;
      this.finish = true;
    });
  },
};
</script>

<style lang="less" scoped>
.container {
  height: 100vh;
  background-color: #f6f6f6;
  .user-info {
    display: flex;
    align-items: center;
    background-color: #fff;
    margin-top: 16px;
    font-size: 12.8px;
    height: 80px;
    .avatar {
      padding: 0 12px;
      img {
        width: 48px;
        height: 48px;
        border-radius: 50%;
      }
    }
  }
  .role {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    padding: 16px;
    .role-item {
      display: flex;
      flex-direction: column;
      justify-content: space-evenly;
      padding: 8px 12px;
      margin-bottom: 16px;
      width: 48%;
      height: 120px;
      border-radius: 8px;
      background-color: #fff;
      &.current {
        border: 1px solid #e1a500;
        background-color: #ffeded;
      }
      .name {
        color: #e1a500;
        font-weight: 700;
      }
      .price {
        font-size: 28px;
        font-weight: 700;
        span {
          font-size: 18px;
        }
      }
      .desc {
        font-size: 12px;
      }
    }
  }
  .buy {
    display: flex;
    align-items: center;
    justify-content: center;
    position: fixed;
    bottom: 0;
    width: 100%;
    height: 48px;
    font-size: 16px;
    color: #fff;
    background-color: #cea200;
  }
}
</style>
